package com.roadjava.rbac.controller;

import com.roadjava.rbac.bean.entity.SuggestionDO;
import com.roadjava.rbac.service.SuggestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/suggestions")
public class SuggestionController {

    @Autowired
    private SuggestionService suggestionService;

    // 添加建议
    @PostMapping
    public ResponseEntity<SuggestionDO> addSuggestion(@RequestBody SuggestionDO suggestion) {
        if (suggestion == null || suggestion.getContent() == null || suggestion.getContent().isEmpty()) {
            return ResponseEntity.badRequest().body(null); // 返回400，参数无效
        }

        suggestionService.addSuggestion(suggestion);
        return ResponseEntity.ok(suggestion); // 返回添加的建议
    }

    // 更新建议
    @PutMapping
    public ResponseEntity<Void> updateSuggestion(@RequestBody SuggestionDO suggestion) {
        if (suggestion == null || suggestion.getSuggestionid() == null) {
            return ResponseEntity.badRequest().build(); // 返回400，参数无效
        }

        suggestionService.updateSuggestion(suggestion);
        return ResponseEntity.ok().build(); // 返回200更新成功
    }

    // 删除建议
    @DeleteMapping
    public ResponseEntity<Void> deleteSuggestion(@RequestParam Integer suggestionId) {
        if (suggestionService.deleteSuggestion(suggestionId)) {
            return ResponseEntity.noContent().build(); // 返回204，删除成功
        } else {
            return ResponseEntity.notFound().build(); // 返回404，未找到
        }
    }

    // 根据ID获取建议
    @GetMapping
    public ResponseEntity<SuggestionDO> getSuggestion(@RequestParam Integer suggestionId) {
        SuggestionDO suggestion = suggestionService.getSuggestionById(suggestionId);
        return suggestion != null ? ResponseEntity.ok(suggestion) : ResponseEntity.notFound().build(); // 返回建议
    }

    // 根据资源ID获取建议列表
    @GetMapping("/resource")
    public ResponseEntity<List<SuggestionDO>> getSuggestionsByResourceId(@RequestParam Integer resourceId) {
        List<SuggestionDO> suggestions = suggestionService.getSuggestionsByResourceId(resourceId);
        return ResponseEntity.ok(suggestions); // 返回获取的建议列表
    }

    // 根据用户ID获取建议列表
    @GetMapping("/user")
    public ResponseEntity<List<SuggestionDO>> getSuggestionsByUserId(@RequestParam Long userId) {
        List<SuggestionDO> suggestions = suggestionService.getSuggestionsByUserId(userId);
        return ResponseEntity.ok(suggestions); // 返回指定用户的建议列表
    }
}
